package com.nbcio.modules.erp.address.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nbcio.modules.erp.address.entity.UserAddresses;
import com.nbcio.modules.erp.address.service.UserAddressesService;
import com.nbcio.modules.erp.address.mapper.UserAddressesMapper;
import org.apache.shiro.util.Assert;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author 31955
* @description 针对表【user_addresses(用户收货地址)】的数据库操作Service实现
* @createDate 2024-04-10 10:16:22
*/
@Service
public class UserAddressesServiceImpl extends ServiceImpl<UserAddressesMapper, UserAddresses>
    implements UserAddressesService{

    @Override
    public void add(Map<String, Object> map) {
//新增收货地址，先赋值，如果是默认地址，再查询该用户下有没有其他的默认地址，有的话改成非默认地址，然后再保存
        UserAddresses userAddresses = new UserAddresses();

        userAddresses.setId(RandomUtil.randomString(32));
        userAddresses.setUserId(getStringVal(map.get("user_id")));
        userAddresses.setName(getStringVal(map.get("userName")));
        userAddresses.setPhone(getStringVal(map.get("tel")));
        userAddresses.setAddressDetail(getStringVal(map.get("streetName")));
        userAddresses.setIsDefault("".equals(getStringVal(map.get("is_default")))?0:Integer.parseInt(map.get("is_default").toString()));

        if (userAddresses.getIsDefault()==1) {
            //查询用户下的收货地址,理应默认地址只能查出一个
            LambdaQueryWrapper<UserAddresses> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserAddresses::getUserId, getStringVal(map.get("user_id")))
                    .eq(UserAddresses::getIsDefault, 1);
            List<UserAddresses> userAddressesList = list(wrapper);
            if (CollectionUtil.isNotEmpty(userAddressesList)) {
                UserAddresses addresses = userAddressesList.get(0);
                addresses.setIsDefault(0);
                updateById(addresses);
            }
        }

        save(userAddresses);
    }

    @Override
    public void edit(Map<String, Object> map) {
//修改收货地址，先赋值，如果是默认地址，再查询该用户下有没有其他的默认地址，有的话改成非默认地址，然后再保存
        Assert.notNull(map.get("id"), "收货地址id不能为空，请联系管理员！");
        UserAddresses userAddresses = getById(map.get("id").toString());

        userAddresses.setId(getStringVal(map.get("id")));
        userAddresses.setName(getStringVal(map.get("userName")));
        userAddresses.setPhone(getStringVal(map.get("tel")));
        userAddresses.setAddressDetail(getStringVal(map.get("streetName")));
        userAddresses.setIsDefault("".equals(getStringVal(map.get("is_default")))? userAddresses.getIsDefault():Integer.parseInt(map.get("is_default").toString()));

        if (userAddresses.getIsDefault()==1) {
            //查询用户下的收货地址,理应默认地址只能查出一个
            LambdaQueryWrapper<UserAddresses> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(UserAddresses::getUserId, getStringVal(map.get("user_id")))
                    .eq(UserAddresses::getIsDefault, 1);
            List<UserAddresses> userAddressesList = list(wrapper);
            if (CollectionUtil.isNotEmpty(userAddressesList)) {
                UserAddresses addresses = userAddressesList.get(0);
                addresses.setIsDefault(0);
                updateById(addresses);
            }
        }

        updateById(userAddresses);
    }

    @Override
    public void delete(Map<String, Object> map) {
        Assert.notNull(map.get("id"), "收货地址id不能为空，请联系管理员！");
        removeById(map.get("id").toString());
    }

    @Override
    public List<UserAddresses> query(Map<String, Object> map) {
        Assert.notNull(map.get("user_id"), "用户id不能为空，请联系管理员！");
        LambdaQueryWrapper<UserAddresses> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserAddresses::getUserId, getStringVal(map.get("user_id")));
        List<UserAddresses> userAddressesList = list(wrapper);

        return userAddressesList;
    }

    String getStringVal(Object object){
        if(object!=null){
            return object.toString();
        }
        return "";
    }
}




